import { cloneDeep } from 'lodash-es'
import { formattingLengStr } from '@/utils/lowDesign';

//将设计器数组配置转换为avue对象配置
const ArrayToObject = (columnList) => {
  const column = {}
  const group: any[] = []
  columnList.forEach(child => {
    if (child.column) {
      if (child.column.length) {
        const { columnData, groupData } = ArrayToObject(child.column)
        child.column = columnData
        if (groupData.length) child.group = groupData
      } else child.column = {}
    }
    if (child.dic) delete child.dic
    if (child.type == 'layoutGroup') group.push(child)
    else {
      const prop = child.prop
      delete child.prop
      column[prop] = child
    }
  })
  return { columnData: column, groupData: group }
}
//将avue对象配置转换为设计器数组配置
const ObjectToArray = (columnObj, groupList?) => {
  const column: any[] = []
  if (!columnObj) columnObj = {}
  if (!groupList) groupList = []
  for (const key in columnObj) {
    if (columnObj[key].column || columnObj[key].group) {
      columnObj[key].column = ObjectToArray(columnObj[key].column, columnObj[key].group)
      delete columnObj[key].group
    }
    column.push({ ...columnObj[key], prop: key })
  }
  groupList.forEach(group => {
    group.column = ObjectToArray(group.column)
    column.push(group)
  })
  return column
}

//配置转换
export const convertFormViewOption = (option, type = 'save') => {
  const data = cloneDeep(option)
  if (type == 'save') {
    const { columnData, groupData } = ArrayToObject(data.column)
    data.column = columnData
    data.group = groupData
  } else if (type == 'formDesign') {
    data.column = ObjectToArray(data.column, data.group)
    delete data.group
  }
  return data
}

//格式化所有的语言配置
export const initI18nData = (optionData, lengData) => {
  const i18nData = {}

  const setI18n = (data, defNameKey, typeKey) => {
    if (data.hideLabel === true) return
    if (!data.i18nCode) data.i18nCode = `${typeKey}_${Math.ceil(Math.random() * 9999999)}`
    i18nData[data.i18nCode] = formattingLengStr(lengData[data.i18nCode] || '', data[defNameKey])
  }

  const eachData = (option, typeKey = 'fields', listKey = 'column', defNameKey = 'label') => {
    option[listKey]?.forEach(item => {
      if (item.type == 'layoutTable') {
        eachData(item)
        const btnKey = ['header', 'menu']
        btnKey.forEach(key => eachData(item.btnData, 'btn', key))
      } else if (['layoutGroup', 'tab', 'comboBox'].includes(item.type)) eachData(item)
      else if (item.type == 'layoutTabs') eachData(item, 'tab')
      else if (item.type == 'buttonList') eachData(item.params, 'btn', 'buttonList')
      else if (item.type == 'monacoEditor') {
        const btnKey = ['headerBtn', 'footerBtn']
        btnKey.forEach(key => eachData(item.params, 'btn', key, 'name'))
      }
      setI18n(item, defNameKey, typeKey)
    })
  }

  eachData(optionData)

  const defI18nKeys = [
    { key: 'config_submitBtn', defKey: 'submitText' },
    { key: 'config_emptyBtn', defKey: 'emptyText' },
    { key: 'config_externalTitle', defKey: 'externalText' },
  ]
  defI18nKeys.forEach(({ key, defKey }) => {
    i18nData[key] = formattingLengStr(lengData[key] || '', optionData[defKey])
  })

  return i18nData
}